{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python Basics\n",
    "==========\n",
    "\n",
    "Because this workshop uses Python, we need to take a little bit of time to explain some basics of programming in Python. A more detailed lesson is available through [Software Carpentry](http://swcarpentry.github.io/python-novice-inflammation/).\n",
    "\n",
    "Importing Python Packages\n",
    "-------------\n",
    "\n",
    "Generally speaking, to import a package ``foo``, you use ``import foo``. Here, we'll work with the ``numpy`` package (aka, _numerical Python_)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, you may wish to use a different name for the package when you call it. (This is especially useful for packages with longer names!) Thankfully, Python import statements support this kind of renaming, using ``import foo as bar``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may also want to import _specific_ functions only, without pulling in the entire package. You can do so using ``from foo import bar``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import linalg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we have access to only functions declared in the ``linalg`` module within ``numpy``.\n",
    "\n",
    "Commenting code\n",
    "------------\n",
    "\n",
    "Few-line comments in Python are preceded by a pound sign:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#A short comment here\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For multi-line comments, use ''' to start the comment, and ''' to close it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "A somewhat more involved\n",
    "multi-line comment \n",
    "'''\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assigning variable names\n",
    "------------\n",
    "\n",
    "Variables in Python are declared and _typed_ dynamically. That is, if I want to create a variable ``height``, and assign it a value of 10, I do"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "height = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I can check the _type_ of height using the ``type()`` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(height)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I can also do arithmetic and other mathematical operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 5.0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "height * 5, height / 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I can assign the output of operations to a new variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "twice_as_high = 2 * height"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we check the value of ``twice_as_high``, it will be two times that of ``height``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "twice_as_high"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suppose we change ``height`` to be 40. What value do you think ``twice_as_high`` will have?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "height = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "twice_as_high"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python variables are not updated dynamically. However, repeated assignments to the same variable name _do_ update its value!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "twice_as_high = 2 * height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "80"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "twice_as_high"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Writing loops\n",
    "-----------\n",
    "\n",
    "Very often, we need to write a loop of some kind to repeat an action. For the purposes of this lesson, the only kind of loop we need to know is the ``for`` loop. This loop takes the form\n",
    "\n",
    "\n",
    "```python\n",
    "for variable in possible_values:\n",
    "    do something\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#Write a loop which prints some integers\n",
    "#by explicitly declaring the list\n",
    "\n",
    "for j in [1, 2, 3, 4, 5]:\n",
    "    print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l\n",
      "a\n",
      "s\n",
      "f\n",
      "d\n",
      "a\n",
      "k\n",
      "l\n",
      "s\n",
      "d\n",
      "f\n",
      "a\n",
      "s\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "#Write a loop which prints the characters of a string\n",
    "for char in 'lasfdaklsdfasd':\n",
    "    print(char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "#Write a loop to count the number of letters in a string.\n",
    "counter = 0\n",
    "\n",
    "for char in 'asdfjasdfkajsd':\n",
    "    #The \"+= 1\" is shorthand for\n",
    "    #add one to the previous value, and\n",
    "    #update the variable\n",
    "    counter += 1\n",
    "    \n",
    "print(counter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
